<template>
  <div class="selectContenner" v-clickoutside="closeHandle">
    <div class="selectBox" @click="selectHandle" :style="setStyle">
      <span class="placeholder" v-if="checkList.length<=0">请选择</span>
      <span v-if="checkDataes.length > 0" >
        <span class="showItem" v-for="it in checkDataes" :key="it.id">
          {{it.name}}
          <!-- <i v-if="it.type == 1" class="all">(全部)</i> -->
          <!-- <i class="del icon" @click.stop="delCheck(it)">&#xe632;</i> -->
        </span>
      </span>
      <span class="lab icon">&#xe61e;</span>
    </div>
    <div class="selectItemsBox" v-show="selectOpen" :style="selStyle">
        <OptionTree :treeDataes="treeDataes" :mareCheck="mareCheck" :checkList="checkList"></OptionTree>
    </div>
  </div>
</template>
<script>
    import OptionTree from './OptionTree'
    import Bus from '@/utils/Bus.js'
    const clickoutside = {
        // 初始化指令
        bind(el, binding, vnode) {
            function documentHandler(e) {
                // 这里判断点击的元素是否是本身，是本身，则返回
                if (el.contains(e.target)) {
                    return false;
                }
                // 判断指令中是否绑定了函数
                if (binding.expression) {
                    // 如果绑定了函数 则调用那个函数，此处binding.value就是handleClose方法
                    binding.value(e);
                }
            }
            // 给当前元素绑定个私有变量，方便在unbind中可以解除事件监听
            el.__vueClickOutside__ = documentHandler;
            document.addEventListener('click', documentHandler);
        },
        update() {},
        unbind(el, binding) {
            // 解除事件监听
            document.removeEventListener('click', el.__vueClickOutside__);
            delete el.__vueClickOutside__;
        },
    };
    export default {
        name: 'SelectTree',
        components:{
            OptionTree
        },
        props: {
            treeDataes: Array,
            closeSelectOpen: Boolean,
            checkNum: Number,
            mareCheck: {
              type:Boolean,
              default: false
            },
            checkListDataes: {
                type: Array,
                default:function(){
                    return []
                }
              },
            dialogVisible: Boolean,
            'setStyle': String,
            'selStyle': String,
            'includeMember':{
                type: Number || String,
                default: 0
            }},
        data() {
            return {
                checkDataList: [],
                checkDataes: [],
                openitems: 2,
                memberlist: [],
                actId:[],
                selectBDataes: [],
                selectCDataes: [],
                selectDDataes: [],
                checked: '',
                selectOpen: false,
                checkList: [],
                checkIdList: [],
                show: true,
            }
        },
        directives: { clickoutside },
        created(){
            // this.getAdminList()
            // this.initCheckList()
            this.onBus()
        },
        watch:{
            dialogVisible(val, oldVal){
                if (!val) {
                  this.checkList = ''
                } else {
                  this.setSelectBox(this.checkNum)
                }
            }
        },
        methods: {
            onBus(){
              Bus.$on('checkHandle', target => this.setSelectBox(target));
            },
            setSelectBox(target){  
                this.checkList = target
                if (target) {
                  if (typeof val != 'object' && this.treeDataes && this.treeDataes.length > 0) {
                      this.checkDataHandle(this.treeDataes, target)
                      this.closeHandle()
                  }
                } else {
                  this.checkDataes = []
                }
                this.$emit('getValue',this.checkList)
            },
            // 处理已选想的实际数据
            checkDataHandle(data, val){
              for(let i = 0; i < data.length; i++){
                if (data[i].id == val) {
                  this.checkDataes = [data[i]]
                  break;
                } else if (data[i].children){
                  this.checkDataHandle(data[i].children, val)
                }
              }
            },
            // 关闭下拉
            closeHandle(){
                if (this.dialogVisible, this.selectOpen) {
                    this.selectOpen = false
                    this.$emit('getValue',this.checkList)
                }
            },
            // 下拉
            selectHandle(){
                this.initCheckList()
                if(this.selectOpen ) {
                    this.selectOpen = false
                    this.$emit('getValue', this.checkList)
                    let idList = []
                    this.checkList.map(n => {
                      idList.push(n.id)
                    })
                    this.checkDataList = idList
                }else {
                    this.selectOpen = true
                }
            },
            
            delCheck(it){const num = this.checkList.indexOf(it)
               num != -1 ?  this.checkList.splice(num, 1) : null
            },
            // 选中 子集关闭
            checkListHander(val, st){
                if (val == this.actId[0] || val == this.actId[1]) {
                    if (st == 'A') {
                        this.checkListSet(this.selectBDataes)
                        this.selectBDataes = ''
                        this.selectCDataes = ''
                        this.actId[0] = ''
                    } else {
                        this.checkListSet(this.selectCDataes)
                        this.selectCDataes = ''
                        this.actId[1] = ''
                    }
                }
                this.$emit('getValue',this.checkList)
            },
            // 初始化数据
            initCheckList(){
                if (this.checkListDataes && this.checkListDataes.length>0) {
                    if (this.checkListDataes[0].id){
                        this.checkList = this.checkListDataes
                    }else{
                        this.checkList = this.checkListDataes.map(n => {
                            return {id: String(n.objectId), name: n.objectName, type: n.objectType}
                        })
                        
                    }
                }
                if (this.checkList.length > 0){
                  this.checkDataList = this.idScreen(this.checkList)
                }
            },
            idScreen(list){
              return list.map(n => n.id || n.objectId)
            },
            // 数据赛选
            checkListSet(obj){
                obj.forEach(n =>{
                    this.checkList.forEach((h, j) => {
                        if (n.id == h.id){
                            this.checkList.splice(j, 1)
                        }
                    })
                })
            }
        }
    }
</script>
<style scoped>
  .show {
    width: 100px;
    height: 100px;
    background-color: red;
  }
</style>
<style lang="scss" scoped>
  .selectContenner {
    display: inline-block;
    position: relative;
    width: 100%;
    .el-checkbox{
      margin-right: 0px;
      .el-checkbox__label{
        padding-left: 5px;
      }
    }
    .selectBox{
      border: solid 1px #e8e9ea;
      position: relative;
      width:calc(100% - 3px);
      line-height: 12px;
      padding: 4px 10px;
      min-height: 30px;
      border-radius: 3px;
      margin: 0 -3px;
      cursor: pointer;
      .placeholder{
        font-size: 14px;
        line-height: 22px;
        color:#DCDFE6
      }
      .showItem{
        position: relative;
        display: inline-block;
        // padding: 4px 10px;
        // background: #DCDFE6;
        color: #666;
        margin: 4px 5px;
        border-radius: 3px;
        font-size: 12px;
        line-height: 12px;
        .all{
          font-size: 10px;
          font-style: normal;
        }
        .del{
          position: relative;
          display: none;
          right: -3px;
          font-size: 12px;
        }
        &:hover .del{
          display: inline-block;
        }
      }
      .lab{
        position: absolute;
        color:#ccc;
        top:10px;
        right:5px;
      }
    }
    .selectItemsBox{
      position: absolute;
      min-width: 100%;
      z-index: 3;
      background: #fff;
      display: inline-block;
      border:solid 1px #ccc;
      border-right: none;
      border-radius: 3px;
      margin-top: 10px;
      padding:0 10px;
    }
    .selectItems{
      display: flex;
      cursor: pointer;
      .itemsBox{
        flex: 1;
        top:0px;
        left: 175px;
        font-size: 14px;
        border-right: solid 1px #ccc;
        min-width: 173px;
        max-height: 200px;
        overflow: scroll;
        .items{
          padding: 0px 10px;
          overflow: hidden;
          line-height: 18px;
          height: 18px;
          margin: 5px 0;
          .act{
            color:#3345ff;
          }
        }
        .items:first-child{
          margin-top: 10px;
        }
        .items:last-child{
          margin-bottom: 10px;
        }
      }
    }
  }
</style>
